package com.ruoyi.exam.domain;

import com.alibaba.dashscope.aigc.generation.Generation;
import com.alibaba.dashscope.aigc.generation.GenerationParam;
import com.alibaba.dashscope.aigc.generation.GenerationResult;
import com.alibaba.dashscope.common.Message;
import com.alibaba.dashscope.common.Role;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.alibaba.dashscope.utils.JsonUtils;
import java.util.*;

public class Test {

   public static GenerationResult callWithMessage() throws ApiException, NoApiKeyException, InputRequiredException {
      Generation gen = new Generation();
      Message systemMsg = Message.builder()
            .role(Role.SYSTEM.getValue())
            .content("You are a helpful assistant.")
            .build();
      Message userMsg = Message.builder()
            .role(Role.USER.getValue())
            .content("你是谁？")
            .build();
      GenerationParam param = GenerationParam.builder()
            .apiKey("sk-828224c476d6466d8e28f40061c80ba6")
            .model("qwen2-7b-instruct")
            .messages(Arrays.asList(systemMsg, userMsg))
            .resultFormat(GenerationParam.ResultFormat.MESSAGE)
            .build();
      return gen.call(param);
   }

   public static void main(String[] args) {
      try {
         GenerationResult result = callWithMessage();
         System.out.println(JsonUtils.toJson(result));
      } catch (ApiException | NoApiKeyException | InputRequiredException e) {
         // 使用日志框架记录异常信息
         System.err.println("An error occurred while calling the generation service: " + e.getMessage());
      }
      System.exit(0);

   }
}
